﻿//********************************************************************************//
//创建日期：<创建日期，2010-08-02>
//创建作者：<王一飞，wangyifei@shanghai3h.com>
//功能说明：SYS_LOGBLL层
//********************************************************************************//
namespace BLL
{
    using System;
    using System.Data;
    using Access;
    using Platform.Data;
    using System.Text;

    using Oracle.DataAccess.Client;

    public class SYS_LOGBLL : SYS_LOGProcess
    {
        #region Field
        private string ConnectionString;
        #endregion

        #region Construction
        public SYS_LOGBLL()
        {
            ConnectionString = base.conn;
        }
        #endregion

        #region  方法
        #region 获取表注释
        private static DataTable _TableComment = null;

        private DataTable TableComment
        {
            get
            {
                if (null == _TableComment)
                {
                    //string sql = "select * from user_tab_comments where TABLE_TYPE ='TABLE'";//update lei 2010-12-16
                    string sql = "select user_tab_comments.* from user_tab_comments,user_tables where TABLE_TYPE ='TABLE' and user_tab_comments.table_name = user_tables.TABLE_NAME";
                    _TableComment = OracleHelper.ExecuteDataset(Platform.Configuration.ConfigHelper.PlatformConnString, CommandType.Text, sql).Tables[0];
                }
                return _TableComment;
            }
        }

        public string TableComments(string filter)
        {
            DataRow[] dr = TableComment.Select("TABLE_NAME='" + filter + "'");
            string comment = "";
            if (dr.Length > 0)
            {
                comment = dr[0]["COMMENTS"].ToString();
            }
            return comment;
        }

        /// <summary>
        /// 根据条件获得所有的表和表注释
        /// </summary>
        /// <param name="sqlwhere"></param>
        /// <returns></returns>
        public DataTable TableCommentByCondition(string sqlwhere)
        {
            DataTable dt = new DataTable();
            //string sql = "select * from user_tab_comments where TABLE_TYPE ='TABLE' AND Table_Name like '%"+sqlwhere+"%'";//update lei 2010-12-16
            string sql = "select user_tab_comments.* from user_tab_comments,user_tables where TABLE_TYPE ='TABLE' and user_tab_comments.table_name = user_tables.TABLE_NAME AND user_tab_comments.Table_Name like '%" + sqlwhere + "%'";
            dt = OracleHelper.ExecuteDataset(Platform.Configuration.ConfigHelper.PlatformConnString, CommandType.Text, sql).Tables[0];
            return dt;
        }



        /// <summary>
        /// 获取所有的表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllTable()
        {
            return this.TableComment;
        }

        #endregion

        #region 获取列注释
        private static DataTable _ColComment = null;
        private DataTable ColComment
        {
            get
            {
                if (null == _ColComment)
                {
                    string sql = "select * from user_col_comments";
                    _ColComment = OracleHelper.ExecuteDataset(Platform.Configuration.ConfigHelper.PlatformConnString, CommandType.Text, sql).Tables[0];
                }
                return _ColComment;
            }
        }

        /// <summary>
        /// 获取列注释,如果没有则返回列名
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public string ColComments(string tableName, string columnName)
        {
            DataRow[] dr = ColComment.Select("TABLE_NAME='" + tableName + "' and COLUMN_NAME='" + columnName + "'");
            string comment = columnName;
            if (dr.Length > 0)
            {
                string[] _splitString = dr[0]["COMMENTS"].ToString().Split('(');
                if (_splitString.Length > 0)
                    comment = _splitString[0].ToString();
                else
                    comment = dr[0]["COMMENTS"].ToString();
            }
            return comment;
        }

        /// <summary>
        /// 根据条件获取列
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public DataTable ColComments(string filter)
        {
            DataRow[] dr = ColComment.Select(filter);
            DataTable dt = ColComment.Clone();
            dt.Rows.Clear();
            for (int i = 0; i <= dr.Length - 1; i++)
            {
                dt.ImportRow((DataRow)dr[i]);
            }
            return dt;
        }

        #endregion

        #region Public Methods
        /// <summary>
        /// 记录登录信息去日志
        /// </summary>
        /// <param name="loginInfo"></param>
        /// <returns></returns>
        public void LogLoginInfo(Entity.LoginInfo loginInfo)
        {
            Log(loginInfo, "登录", Entity.OperateState.Add, loginInfo, null);
        }


        /// <summary>
        /// 判断在实体中是否存在指定列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="columnname"></param>
        /// <returns></returns>
        private bool IsExistColumnInEntity<T>(T entity, string columnname)
        {
            Type type = entity.GetType();
            if (ColComments(type.Name, columnname).Trim() != columnname)
                return true;
            return false;
        }

        /// <summary>
        /// 记录系统日志，由于有些操作比较复杂，所以允许直接插入Message
        /// </summary>
        /// <param name="loginInfo">用户登录账户</param>
        /// <param name="modelName">模块（比如册本管理模块等）</param>
        /// <param name="tableName">表名如果没有表名可写人存储过程名等</param>
        /// <param name="yongHuH">用户号</param>
        /// <param name="laiHanBH">来函编号</param> 
        /// <param name="opType">操作类型（新增，修改,删除）</param>
        /// <param name="message">消息</param>
        /// <param name="transaction">事务</param>
        /// <returns></returns>
        public bool Log(Entity.LoginInfo loginInfo, string modelName, string yongHuH, string laiHanBH, Entity.OperateState opType, string message, int kid, OracleTransaction transaction)
        {

            Entity.SYS_LOG log = new Entity.SYS_LOG();
            log.S_CAOZUOR = loginInfo.Account;
            log.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            log.S_BIAOMING = "Message";
            log.S_SUOSHUMK = modelName;
            log.S_IP = loginInfo.IP;
            log.S_YONGHUH = yongHuH;
            log.S_LAIHANBH = laiHanBH;
            log.I_KID = kid;
            message = message.Replace("'", "‘");


            //表示添加
            if (opType == Entity.OperateState.Add)
            {
                log.S_CAOZUOLX = "添加";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<Message " + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "做了" + message + "添加操作'>" + System.Environment.NewLine);
                logComment.Append("</Message>");
                log.S_RIZHINR = logComment.ToString();
            }
            else if (opType == Entity.OperateState.Modify)
            {
                log.S_CAOZUOLX = "修改";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<Message " + "title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "做了" + message + "修改操作'>" + System.Environment.NewLine);
                logComment.Append("</Message>");
                log.S_RIZHINR = logComment.ToString();
            }
            else if (opType == Entity.OperateState.Delete)
            {
                log.S_CAOZUOLX = "删除";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<Message " + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "做了" + message + "删除操作'>" + System.Environment.NewLine);
                logComment.Append("</Message>");
                log.S_RIZHINR = logComment.ToString();
            }

            bool result = false;
            if (transaction == null)
                result = InsertOrUpdate(log);
            else
                result = InsertOrUpdate(log, transaction);

            return result;

        }

        /// <summary>
        /// 记录系统日志，由于有些操作比较复杂，所以允许直接插入Message
        /// </summary>
        /// <param name="loginInfo">用户登录账户</param>
        /// <param name="modelName">模块（比如册本管理模块等）</param>
        /// <param name="tableName">表名如果没有表名可写人存储过程名等</param>
        /// <param name="keHuID">客户编号</param>
        /// <param name="cid">销根号</param>
        /// <param name="feeid">账号</param>
        /// <param name="opType">操作类型（新增，修改,删除）</param>
        /// <param name="message">消息</param>
        /// <param name="transaction">事务</param>
        public bool Log(Entity.LoginInfo loginInfo, string modelName, string yongHuH, string laiHanBH, Entity.OperateState opType, string message, int kid)
        {
            return Log(loginInfo, modelName, yongHuH, laiHanBH, opType, message, kid, null);
        }


        /// <summary>
        /// 记录系统日志
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="account">用户登录账户</param>
        /// <param name="modelName">模块 </param>
        /// <param name="opType">操作类型（新增，1，修改，2）</param>
        /// <param name="entity">实体</param>
        /// <param name="transaction">事务</param>
        public bool Log<T>(Entity.LoginInfo loginInfo, string modelName, Entity.OperateState opType, T entity, OracleTransaction transaction)
        {

            Type type = entity.GetType();
            Entity.SYS_LOG log = new Entity.SYS_LOG();
            log.S_CAOZUOR = loginInfo.Account;
            log.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            log.S_BIAOMING = type.Name;
            log.S_SUOSHUMK = modelName;
            log.S_IP = loginInfo.IP;
            try
            {
                log.S_YONGHUH = IsExistColumnInEntity(entity, "S_YONGHUH") == true ? type.GetProperty("S_YONGHUH").GetValue(entity, null).ToString() : null;
            }
            catch
            {
                log.S_YONGHUH = null;
            }
            try
            {
                log.S_LAIHANBH = IsExistColumnInEntity(entity, "S_LAIHANBH") == true ? type.GetProperty("S_LAIHANBH").GetValue(entity, null).ToString() : null;
            }
            catch
            {
                log.S_LAIHANBH = null;
            }
            try
            {
                if (IsExistColumnInEntity(entity, "I_KID"))
                    log.I_KID = Convert.ToInt32(type.GetProperty("I_KID").GetValue(entity, null).ToString());
            }
            catch
            {
                log.I_KID = 0;
            }

            try
            {
                log.S_ZHUJIAN = type.GetProperty("ID").GetValue(entity, null).ToString();
            }
            catch
            {
                log.S_ZHUJIAN = "";
            }
            //表示添加
            if (opType == Entity.OperateState.Add)
            {
                log.S_CAOZUOLX = "添加";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<" + log.S_BIAOMING + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "添加一条记录'>" + System.Environment.NewLine);
                logComment.Append(CreateXmlLog(opType, entity));
                logComment.Append("</" + log.S_BIAOMING + ">");
                log.S_RIZHINR = logComment.ToString();
            }
            else if (opType == Entity.OperateState.Modify)
            {
                log.S_CAOZUOLX = "修改";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<" + log.S_BIAOMING + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "修改一条记录'>" + System.Environment.NewLine);
                logComment.Append(CreateXmlLog(opType, entity));
                logComment.Append("</" + log.S_BIAOMING + ">");
                log.S_RIZHINR = logComment.ToString();
            }
            else if (opType == Entity.OperateState.Delete)
            {
                log.S_CAOZUOLX = "删除";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<" + log.S_BIAOMING + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "删除一条记录'>" + System.Environment.NewLine);
                logComment.Append(Log(opType, int.Parse(log.S_ZHUJIAN), log.S_BIAOMING));
                logComment.Append("</" + log.S_BIAOMING + ">");
                log.S_RIZHINR = logComment.ToString();
            }

            bool result = false;
            if (transaction == null)
                result = InsertOrUpdate(log);
            else
                result = InsertOrUpdate(log, transaction);

            return result;
        }


        /// <summary>
        /// 记录系统日志
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="account">用户登录账户</param>
        /// <param name="modelName">模块 </param>
        /// <param name="opType">操作类型（新增，1，修改，2）</param>
        /// <param name="entity">实体</param>
        /// <param name="transaction">事务</param>
        public bool Log<T>(Entity.LoginInfo loginInfo, string yewulx, string modelName, Entity.OperateState opType, T entity, OracleTransaction transaction)
        {

            Type type = entity.GetType();
            Entity.SYS_LOG log = new Entity.SYS_LOG();
            log.S_CAOZUOR = loginInfo.UserID.ToString();
            log.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            log.S_BIAOMING = type.Name;
            log.S_SUOSHUMK = modelName;
            log.S_IP = loginInfo.IP;
            log.S_YEWULX = yewulx;
            try
            {
                log.S_YONGHUH = IsExistColumnInEntity(entity, "S_YONGHUH") == true ? type.GetProperty("S_YONGHUH").GetValue(entity, null).ToString() : null;
            }
            catch
            {
                log.S_YONGHUH = null;
            }
            try
            {
                log.S_LAIHANBH = IsExistColumnInEntity(entity, "S_LAIHANBH") == true ? type.GetProperty("S_LAIHANBH").GetValue(entity, null).ToString() : null;
            }
            catch
            {
                log.S_LAIHANBH = null;
            }
            try
            {
                if (IsExistColumnInEntity(entity, "I_KID"))
                    log.I_KID = Convert.ToInt32(type.GetProperty("I_KID").GetValue(entity, null).ToString());
            }
            catch
            {
                log.I_KID = 0;
            }

            try
            {
                log.S_ZHUJIAN = type.GetProperty("ID").GetValue(entity, null).ToString();
            }
            catch
            {
                log.S_ZHUJIAN = "";
            }
            //表示添加
            if (opType == Entity.OperateState.Add)
            {
                log.S_CAOZUOLX = "添加";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<" + log.S_BIAOMING + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "添加一条记录'>" + System.Environment.NewLine);
                logComment.Append(CreateXmlLog(opType, entity));
                logComment.Append("</" + log.S_BIAOMING + ">");
                log.S_RIZHINR = logComment.ToString();
            }
            else if (opType == Entity.OperateState.Modify)
            {
                log.S_CAOZUOLX = "修改";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<" + log.S_BIAOMING + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "修改一条记录'>" + System.Environment.NewLine);
                logComment.Append(CreateXmlLog(opType, entity));
                logComment.Append("</" + log.S_BIAOMING + ">");
                log.S_RIZHINR = logComment.ToString();
            }
            else if (opType == Entity.OperateState.Delete)
            {
                log.S_CAOZUOLX = "删除";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<" + log.S_BIAOMING + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "删除一条记录'>" + System.Environment.NewLine);
                logComment.Append(Log(opType, int.Parse(log.S_ZHUJIAN), log.S_BIAOMING));
                logComment.Append("</" + log.S_BIAOMING + ">");
                log.S_RIZHINR = logComment.ToString();
            }

            bool result = false;
            if (transaction == null)
                result = InsertOrUpdate(log);
            else
                result = InsertOrUpdate(log, transaction);

            return result;
        }


        public bool Log<T>(string userId, string yewulx, string modelName, Entity.OperateState opType, T entity, OracleTransaction transaction)
        {

            Type type = entity.GetType();
            Entity.SYS_LOG log = new Entity.SYS_LOG();
            log.S_CAOZUOR = userId;
            log.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            log.S_BIAOMING = type.Name;
            log.S_SUOSHUMK = modelName;
           // log.S_IP = loginInfo.IP;
            log.S_YEWULX = yewulx;
            try
            {
                log.S_YONGHUH = IsExistColumnInEntity(entity, "S_YONGHUH") == true ? type.GetProperty("S_YONGHUH").GetValue(entity, null).ToString() : null;
            }
            catch
            {
                log.S_YONGHUH = null;
            }
            try
            {
                log.S_LAIHANBH = IsExistColumnInEntity(entity, "S_LAIHANBH") == true ? type.GetProperty("S_LAIHANBH").GetValue(entity, null).ToString() : null;
            }
            catch
            {
                log.S_LAIHANBH = null;
            }
            try
            {
                if (IsExistColumnInEntity(entity, "I_KID"))
                    log.I_KID = Convert.ToInt32(type.GetProperty("I_KID").GetValue(entity, null).ToString());
            }
            catch
            {
                log.I_KID = 0;
            }

            try
            {
                log.S_ZHUJIAN = type.GetProperty("ID").GetValue(entity, null).ToString();
            }
            catch
            {
                log.S_ZHUJIAN = "";
            }
            //表示添加
            if (opType == Entity.OperateState.Add)
            {
                log.S_CAOZUOLX = "添加";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<" + log.S_BIAOMING + " title='" + userId + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "添加一条记录'>" + System.Environment.NewLine);
                logComment.Append(CreateXmlLog(opType, entity));
                logComment.Append("</" + log.S_BIAOMING + ">");
                log.S_RIZHINR = logComment.ToString();
            }
            else if (opType == Entity.OperateState.Modify)
            {
                log.S_CAOZUOLX = "修改";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<" + log.S_BIAOMING + " title='" + userId + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "修改一条记录'>" + System.Environment.NewLine);
                logComment.Append(CreateXmlLog(opType, entity));
                logComment.Append("</" + log.S_BIAOMING + ">");
                log.S_RIZHINR = logComment.ToString();
            }
            else if (opType == Entity.OperateState.Delete)
            {
                log.S_CAOZUOLX = "删除";
                StringBuilder logComment = new StringBuilder();
                logComment.Append("<" + log.S_BIAOMING + " title='" + userId + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "删除一条记录'>" + System.Environment.NewLine);
                logComment.Append(Log(opType, int.Parse(log.S_ZHUJIAN), log.S_BIAOMING));
                logComment.Append("</" + log.S_BIAOMING + ">");
                log.S_RIZHINR = logComment.ToString();
            }

            bool result = false;
            if (transaction == null)
                result = InsertOrUpdate(log);
            else
                result = InsertOrUpdate(log, transaction);

            return result;
        }

        //public bool Log<T>(Entity.LoginInfo loginInfo, WordsStruct.I_YEWULX yewulx, string modelName, Entity.OperateState opType, T entity, OracleTransaction transaction)
        //{

        //    Type type = entity.GetType();
        //    Entity.SYS_LOG log = new Entity.SYS_LOG();
        //    log.S_CAOZUOR = loginInfo.Account;
        //    log.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
        //    log.S_BIAOMING = type.Name;
        //    log.S_SUOSHUMK = modelName;
        //    log.S_IP = loginInfo.IP;
        //    log.S_YEWULX = yewulx.ToString();
        //    try
        //    {
        //        log.S_YONGHUH = IsExistColumnInEntity(entity, "S_YONGHUH") == true ? type.GetProperty("S_YONGHUH").GetValue(entity, null).ToString() : null;
        //    }
        //    catch
        //    {
        //        log.S_YONGHUH = null;
        //    }
        //    try
        //    {
        //        log.S_LAIHANBH = IsExistColumnInEntity(entity, "S_LAIHANBH") == true ? type.GetProperty("S_LAIHANBH").GetValue(entity, null).ToString() : null;
        //    }
        //    catch
        //    {
        //        log.S_LAIHANBH = null;
        //    }
        //    try
        //    {
        //        if (IsExistColumnInEntity(entity, "I_KID"))
        //            log.I_KID = Convert.ToInt32(type.GetProperty("I_KID").GetValue(entity, null).ToString());
        //    }
        //    catch
        //    {
        //        log.I_KID = 0;
        //    }

        //    try
        //    {
        //        log.S_ZHUJIAN = type.GetProperty("ID").GetValue(entity, null).ToString();
        //    }
        //    catch
        //    {
        //        log.S_ZHUJIAN = "";
        //    }
        //    //表示添加
        //    if (opType == Entity.OperateState.Add)
        //    {
        //        log.S_CAOZUOLX = "添加";
        //        StringBuilder logComment = new StringBuilder();
        //        logComment.Append("<" + log.S_BIAOMING + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "添加一条记录'>" + System.Environment.NewLine);
        //        logComment.Append(CreateXmlLog(opType, entity));
        //        logComment.Append("</" + log.S_BIAOMING + ">");
        //        log.S_RIZHINR = logComment.ToString();
        //    }
        //    else if (opType == Entity.OperateState.Modify)
        //    {
        //        log.S_CAOZUOLX = "修改";
        //        StringBuilder logComment = new StringBuilder();
        //        logComment.Append("<" + log.S_BIAOMING + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "修改一条记录'>" + System.Environment.NewLine);
        //        logComment.Append(CreateXmlLog(opType, entity));
        //        logComment.Append("</" + log.S_BIAOMING + ">");
        //        log.S_RIZHINR = logComment.ToString();
        //    }
        //    else if (opType == Entity.OperateState.Delete)
        //    {
        //        log.S_CAOZUOLX = "删除";
        //        StringBuilder logComment = new StringBuilder();
        //        logComment.Append("<" + log.S_BIAOMING + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "删除一条记录'>" + System.Environment.NewLine);
        //        logComment.Append(Log(opType, int.Parse(log.S_ZHUJIAN), log.S_BIAOMING));
        //        logComment.Append("</" + log.S_BIAOMING + ">");
        //        log.S_RIZHINR = logComment.ToString();
        //    }

        //    bool result = false;
        //    if (transaction == null)
        //        result = InsertOrUpdate(log);
        //    else
        //        result = InsertOrUpdate(log, transaction);

        //    return result;
        //}

        /// <summary>
        /// 记录系统日志
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="account">用户登录账户</param>
        /// <param name="modelName">模块（比如册本管理模块等）</param>
        /// <param name="opType">操作类型（删除）</param>
        /// <param name="int">ID</param>
        /// <param name="string">表名</param>
        /// <param name="OracleTransaction">OracleTransaction</param>
        public bool Log(Entity.LoginInfo loginInfo, string yewulx, string modelName, int id, string tableName, OracleTransaction transaction)
        {
            Entity.SYS_LOG log = new Entity.SYS_LOG();
            log.S_CAOZUOR = loginInfo.Account;
            log.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            log.S_BIAOMING = tableName;
            log.S_SUOSHUMK = modelName;
            log.S_ZHUJIAN = id.ToString();
            log.S_IP = loginInfo.IP;
            log.S_YEWULX = yewulx;
            log.S_CAOZUOLX = "删除";
            StringBuilder logComment = new StringBuilder();
            logComment.Append("<" + tableName + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(tableName) + "删除一条记录'>" + System.Environment.NewLine);
            logComment.Append(Log(Entity.OperateState.Delete, id, tableName));
            logComment.Append("</" + tableName + ">");
            log.S_RIZHINR = logComment.ToString();

            bool result = false;
            if (transaction == null)
                result = InsertOrUpdate(log);
            else
                result = InsertOrUpdate(log, transaction);

            return result;
        }

        /// <summary>
        /// 条件删除日志
        /// </summary>
        /// <param name="loginInfo"></param>
        /// <param name="modelName"></param>
        /// <param name="tableName"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public bool Log(Entity.LoginInfo loginInfo, string modelName, string tableName, OracleTransaction transaction)
        {
            Entity.SYS_LOG log = new Entity.SYS_LOG();
            log.S_CAOZUOR = loginInfo.Account;
            log.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            log.S_BIAOMING = tableName;
            log.S_SUOSHUMK = modelName;
            log.S_IP = loginInfo.IP;

            log.S_CAOZUOLX = "删除";
            StringBuilder logComment = new StringBuilder();
            logComment.Append("<" + tableName + " title='" + loginInfo.Account + "于" + BusinessTool.GetDataBaseDateTime().ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(tableName) + "删除一条记录'>" + System.Environment.NewLine);
            logComment.Append("</" + tableName + ">");
            log.S_RIZHINR = logComment.ToString();
            bool result = false;
            if (transaction == null)
                result = InsertOrUpdate(log);
            else
                result = InsertOrUpdate(log, transaction);

            return result;
        }

        /// <summary>
        /// 根据表名和主键ID获取日志字符串
        /// </summary>
        /// <param name="opType"></param>
        /// <param name="id"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private string Log(Entity.OperateState opType, int id, string tableName)
        {
            string s = "";
            if (opType == Entity.OperateState.Delete)
            {
                DataTable dt = BusinessTool.GetDataTable(tableName, id);
                for (int i = 0; i < dt.Columns.Count && dt.Rows.Count > 0; i++)
                {
                    string columnComment = ColComments(tableName, dt.Columns[i].ColumnName);
                    s += " <" + tableName + "." + dt.Columns[i].ColumnName + " name='" + columnComment + "' oldvalue='" + ChangeValue(NullToString(dt.Rows[0][i])) + "' newvalue=''/>" + System.Environment.NewLine;
                }
            }
            return s;
        }

        /// <summary>
        /// 将数据Log数据保存为XML文档形式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="opType"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        private string CreateXmlLog<T>(Entity.OperateState opType, T entity)
        {
            string s = "";
            if (opType == Entity.OperateState.Add)
            {
                Type type = entity.GetType();
                string tableName = type.Name;
                foreach (System.Reflection.PropertyInfo p in type.GetProperties())
                {
                    object o = p.GetValue(entity, null);
                    string columnName = p.Name;
                    string columnComment = ColComments(tableName, columnName);
                    s += " <" + tableName + "." + columnName + " name='" + columnComment + "' oldvalue='' newvalue='" + ChangeValue(NullToString(o)) + "'/>" + System.Environment.NewLine;
                }
            }
            else if (opType == Entity.OperateState.Modify)
            {
                Type type = entity.GetType();
                string tableName = type.Name;

                int id = 0;
                try
                {
                    id = int.Parse(type.GetProperty("ID").GetValue(entity, null).ToString());
                }
                catch { }

                DataTable dt = BusinessTool.GetDataTable(type.Name, id);
                T entityOld = (T)Activator.CreateInstance(type);
                entityOld = ConvertDataTable(dt, entityOld);
                Type typeOld = entityOld.GetType();
                int count = type.GetProperties().Length;
                for (int i = 0; i < count; i++)
                {
                    object o = type.GetProperties()[i].GetValue(entity, null);
                    object oOld = typeOld.GetProperties()[i].GetValue(entityOld, null);
                    if (NullToString(oOld) != NullToString(o))
                    {
                        string columnName = type.GetProperties()[i].Name;
                        string columnComment = ColComments(tableName, columnName);
                        s += " <" + tableName + "." + columnName + " name='" + columnComment + "' oldvalue='" + ChangeValue(NullToString(oOld)) + "' newvalue='" + ChangeValue(NullToString(o)) + "'/>" + System.Environment.NewLine;
                    }
                }
            }
            return s;
        }

        /// <summary>
        /// 改变转移字符
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private string ChangeValue(string value)
        {
            string result = value;
            if (result.Contains("&"))
            {
                result = result.Replace("&", "&amp;");
            }
            if (result.Contains(">"))
            {
                result = result.Replace(">", "&gt;");
            }
            if (result.Contains("<"))
            {
                result = result.Replace("<", "&lt;");
            }
            if (result.Contains("\""))
            {
                result = result.Replace("\"", "&quot;");
            }
            if (result.Contains("'"))
            {
                result = result.Replace("'", "&apos;");
            }
            if (result.Contains("-"))
            {
                result = result.Replace("-", "\\-");
            }
            return result;
        }

        /// <summary>
        /// 根据实体获取日志字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="opType"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        private string Log<T>(Entity.OperateState opType, T entity)
        {
            string s = "";

            if (opType == Entity.OperateState.Add)
            {
                Type type = entity.GetType();
                string tableName = type.Name;
                foreach (System.Reflection.PropertyInfo p in type.GetProperties())
                {
                    object o = p.GetValue(entity, null);
                    string columnName = p.Name;
                    string columnComment = ColComments(tableName, columnName);
                    s += columnComment + ":" + NullToString(o) + System.Environment.NewLine;
                }
            }
            else if (opType == Entity.OperateState.Modify)
            {
                Type type = entity.GetType();
                string tableName = type.Name;

                int id = 0;
                try
                {
                    id = int.Parse(type.GetProperty("ID").GetValue(entity, null).ToString());
                }
                catch { }

                DataTable dt = BusinessTool.GetDataTable(type.Name, id);
                T entityOld = (T)Activator.CreateInstance(type);
                entityOld = ConvertDataTable(dt, entityOld);
                Type typeOld = entityOld.GetType();
                int count = type.GetProperties().Length;
                for (int i = 0; i < count; i++)
                {
                    object o = type.GetProperties()[i].GetValue(entity, null);
                    object oOld = typeOld.GetProperties()[i].GetValue(entityOld, null);
                    if (NullToString(oOld) != NullToString(o))
                    {
                        string columnName = type.GetProperties()[i].Name;
                        string columnComment = ColComments(tableName, columnName);
                        s += columnComment + ":" + "从" + NullToString(oOld) + "变为" + NullToString(o) + System.Environment.NewLine;
                    }
                }
            }
            return s;
        }

        /// <summary>
        /// 将NULL转字符串
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        private static string NullToString(object o)
        {
            if (null == o)
            {
                return "null";
            }
            return o.ToString();
        }

        /// <summary>
        /// 将单行DataTable转Entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        private T ConvertDataTable<T>(DataTable dt, T entity)
        {
            Type type = entity.GetType();
            DataRow dr = dt.Rows[0];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                object o = dr[i];
                string name = dt.Columns[i].ColumnName;
                if (o != DBNull.Value)
                {
                    try
                    {   //给属性赋值


                        Type t = type.GetProperty(name).PropertyType;

                        if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
                            t = t.GetGenericArguments()[0];


                        type.GetProperty(name).SetValue(entity, Convert.ChangeType(o, t), null);
                    }
                    catch //如果有错误,继续下一个属性的赋值   
                    {
                        continue;
                    }
                }
            }
            return entity;
        }

        /// <summary>
        /// 根据查询条件重构方法
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public DataTable GetTuoShouXXLogList(string value)
        {
            string whereCondition = "";
            whereCondition += "S_BIAOMING='KG_TUOSHOUXX' and S_ZHUJIAN='" + value + "'";
            return this.SelectSYS_LOGsDynamic(whereCondition, "ID");
        }

        ///// 根据查询条件查出日志信息
        ///// </summary>
        ///// <param name="parameterList">查询条件组成的查询词典列表</param>
        ///// <param name="order">排序字段</param>
        ///// <returns>满足条件的日志信息表</returns>
        //public DataTable SelectSYS_LOGsDynamic( ParameterList parameterList, string order)
        //{
        //    return this.SelectSYS_LOGsDynamic(parameterList.ToString(), order);
        //}

        /// <summary>
        /// 分页控件事件查询数据
        /// </summary>
        /// <param name="condtionString">sql集合</param>
        /// <param name="currentPage">默认记录</param>
        /// <param name="pageCount">最大记录数</param>
        /// <returns>DataSet</returns>
        public DataSet SelectSYS_LOGsDynamic(string condtionString, int currentPage, int pageCount)
        {

            return null;
        }

        /// <summary>
        /// 分页控件获得最大记录数
        /// </summary>
        /// <param name="condtionString"></param>
        /// <returns></returns>
        public int GetAllLog(string condtionString)
        {
            string sql = "select count(*) from SYS_LOG where " + condtionString;
            DataSet ds = OracleHelper.ExecuteDataset(conn, CommandType.Text, sql);
            int count = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            return count;
        }


        #endregion
        #endregion
    }
}